﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TPCN.DAL
{
    public class ChiTietDonDatHangDAL
    {
        public static ChiTietDonDatHangObj GetByID(string ChitietdondathangID)
        {
            ChiTietDonDatHangObj obj = new ChiTietDonDatHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbChiTietDonDatHang_GetByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ChitietdondathangID", ChitietdondathangID);
                SqlDataReader dr = Database.db.GetDataToDataReader(cmd);
                if (dr.HasRows)
                {
                    dr.Read();
                    obj = obj.SetValuesFromDataReader(dr);
                }
                dr.Close();
            }
            return obj;
        }

        public static List<ChiTietDonDatHangObj> GetByAll()
        {
            List<ChiTietDonDatHangObj> result = new List<ChiTietDonDatHangObj>();
            ChiTietDonDatHangObj obj = new ChiTietDonDatHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbChiTietDonDatHang_GetByAll";
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static List<ChiTietDonDatHangObj> GetByOrderID(string orderID)
        {
            List<ChiTietDonDatHangObj> result = new List<ChiTietDonDatHangObj>();
            ChiTietDonDatHangObj obj = new ChiTietDonDatHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbChiTietDonDatHang_GetByDonDatHangID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@DondathangID", orderID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static bool Insert(ChiTietDonDatHangObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbChiTietDonDatHang_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DondathangID", (obj.DondathangID == null) ? String.Empty : obj.DondathangID);
                    cmd.Parameters.AddWithValue("@HangID", (obj.HangID == null) ? String.Empty : obj.HangID);
                    cmd.Parameters.AddWithValue("@Soluong", (obj.Soluong == null) ? "0" : obj.Soluong);
                    cmd.Parameters.AddWithValue("@Ghichu", (obj.Ghichu == null) ? String.Empty : obj.Ghichu);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Update(ChiTietDonDatHangObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbChiTietDonDatHang_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ChitietdondathangID", (obj.ChitietdondathangID == null) ? String.Empty : obj.ChitietdondathangID);
                    cmd.Parameters.AddWithValue("@DondathangID", (obj.DondathangID == null) ? String.Empty : obj.DondathangID);
                    cmd.Parameters.AddWithValue("@HangID", (obj.HangID == null) ? String.Empty : obj.HangID);
                    cmd.Parameters.AddWithValue("@Soluong", (obj.Soluong == null) ? "0" : obj.Soluong);
                    cmd.Parameters.AddWithValue("@Ghichu", (obj.Ghichu == null) ? String.Empty : obj.Ghichu);

                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Delete(string ChitietdondathangID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_tbChiTietDonDatHang_DeleteByID";
                    cmd.Parameters.AddWithValue("@ChitietdondathangID", ChitietdondathangID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool DeleteByOrderID(string DondathangID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_tbChiTietDonDatHang_DeleteByDonDatHangID";
                    cmd.Parameters.AddWithValue("@DondathangID", DondathangID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
